import { postAction,delAction } from "@/utils/plugins/request.js";
import {OPT_ADD,OPT_EDIT,OPT_DETAIL} from '@/utils/const/enum.js'
export default {
  data() {
    return {
      listUrl: "/mbap-pp/rest/system/role",
      delUrl: "/232775/example/1570349707210",
      editUrl: "",
      selectedRowKeys: [],
      paging: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
      data: [],
      params: {
        pageNo: 1,
        pageSize: 10,
      },
      searchParams: null,
      loading: false,
      title: "新增",
      visible: false,
      id: "",
      type:"",
    };
  },
  mounted() {
    this.initPage();
  },
  methods: {
    /**
     * 统一处理请求，如不合适，重写此方法即刻
     */
    initPage() {
      this.loading = true;
      postAction(this.listUrl, this.params).then((res) => {
        this.loading = false;
        this.data = res.data.rows;
        this.paging.total = res.data.total;
      });
    },
    /**
     * 页面分页发生改变
     * @param {*} pagination 分页的配置
     */
    tableChange(pagination) {
      this.paging = pagination;
      this.params.pageNo = pagination.current;
      this.initPage();
    },
    loadData(){
      this.initPage();
    },
    /**
     * 搜索的配置数据
     * @param {*} searchForm  搜索的表单
     */
    search(searchParams) {
      this.params = { ...this.params, ...searchParams };
      this.initPage();
    },
    /**
     * 单个删除的处理
     * @param {*} id
     */
    delHandler(id) {
      this.delData([id]);
    },
    /**
     * 修改页面的处理
     * @param {*} item
     */
    editHandler(item) {
      this.title = "修改";
      this.id = item.id;
      this.visible = true;
      this.type = OPT_EDIT
    },
    detailHandler(item){
      this.title = "详情";
      this.id = item.id;
      this.visible = true;
      this.type = OPT_DETAIL
    },
    /**
     * 选中多个数据的change 事件
     * @param {*} selectedRowKeys
     */
    onSelectChange(selectedRowKeys) {
      if (selectedRowKeys.length) {
        this.$emit("delAll-height", 78);
      } else {
        this.$emit("delAll-height", 30);
      }
      console.log(this.selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },
    /**
     * 删除数据的请求
     * @param {*} ids
     */
    delData(ids) {
      if(!ids.length){
          return
      }
      this.$confirm({
        title: "是否删除该内容?",
        content: "删除后无法恢复",
        okText: "删除",
        okType: "danger",
        cancelText: "取消",
        onOk: () => {
          delAction(this.delUrl,{},ids).then(res=>{
            this.$notification['success']({
              message: res.message
            })
          })
        },
      });
    },
    /**
     * 删除选中的数据
     */
    delSelectData() {
      console.log(this.selectedRowKeys);
      this.delData(this.selectedRowKeys);
    },
    /**
     * 新增或修改关闭
     */
    onClose(e) {
      this.visible = false;
    },
    /**
     * 添加数据的函数
     */
    addHandler() {
      if (this.id) {
        this.id = "";
      }
      this.visible = true;
      this.title = "新增";
      this.type = OPT_ADD
    },
  },
  computed: {
    hasSelected() {
      return this.selectedRowKeys.length > 0;
    },
  },
};
